<?php
/*
 * @project CreativeFramework
 * @version <#RELEASE_VERSION#>
 * @license GNU Lesser General Public License
 * @author Jimmie Lin
 */
class Groups_model extends CF_Model{
	/*
	 * bool create
	 * @desc Creates a group.
	 * @param string name Group Name
	 * @param string desc Group Description
	 * @param int pmask Permission Mask
	 * @param string pre Group Display Prefix
	 * @param string suf Group Display Suffix
	 * @param bool is_admin=false Is Admin
	 * @param bool is_smod=false  Is S.Moderator
	 * @param bool is_banned=false Is Banned
	 */
	function create($name, $desc, $pmask, $pre, $suf, $is_admin=false, $is_smod=false, $is_banned=false) {
		global $DB, $Encryption;
		$data = array(
			"name" => $name,
			"desc" => $desc,
			"permissionmask" => $pmask,
			"group_display_prefix" => $pre,
			"group_display_suffix" => $suf,
			"is_admin" => ($is_admin == true ? 1 : 0),
			"is_smod" => ($is_smod == true ? 1 : 0),
			"is_banned" => ($is_banned == true ? 1 : 0),
			"is_native" => 0
		);
		$q = $DB->Driver->insert_query("groups", $data);
		return $q;
	}
	
	/*
	 * array retrieve
	 * @desc Retrieves group(s) using a determined mode
	 * @param string mode="all"
	 * @param string data=""
	 * @mode all: Selects all groups
	 * @mode singleid: Pass GID to data: Selects group with GID
	 */
	function retrieve($mode = "all", $data = "") {
		global $DB;
		// Little Quirks to avoid mistyping
		if(!IN_STRICT) {
			if($mode == "singleuid") $mode = "singleid";
			if($mode == "singlename") $mode = "singleuname";
		} else {
			if($mode == "singleuid" or $mode == "singlename") trigger_error("STRICT Error: Strict mode does not support 'singleuid' or 'singlename' modes, use 'singleid' and 'singleuname', respectively");
		}
		switch($mode) {
			case 'all':
				$query = $DB->Driver->simple_select("groups");
				if($DB->Driver->num_rows($query) == 0) return false;
				else {
					while($array = $DB->Driver->fetch_array($query)) {
						$data[] = $array;
					}
					return $data;
				}
			break;
			case 'singleid':
				$query = $DB->Driver->simple_select("groups", "*", "`id` = {$data}");
				if($DB->Driver->num_rows($query) == 0) return false;
				else return $DB->Driver->fetch_array($query);
			break;
		}
	}
	
	/*
	 * bool update
	 * @desc Updates a determined group using a determined mode
	 * @param array identifier
	 * @param array data
	 * @identifier array = [0] => string id, [1] => mixed value (id)
	 * @data array [key] => value, [key] => value...
	 */
	function update($identifier, $data) {
		global $DB;
		return $DB->Driver->update_query("groups", $identifier, $data);
	}
	
	/*
	 * bool delete
	 * @desc Deletes a determined group using a determined mode
	 * @param array identifier
	 * @identifier array = [0] => string id, [1] => int value (id)
	 */
	function delete($identifier) {
		global $DB;
		// TODO: Check for native
		return $DB->Driver->delete_query("groups", $identifier);
	}

}
